package org.edu.modules.pyfa.controller;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.models.auth.In;
import org.apache.commons.lang.StringUtils;
import org.edu.common.constant.CommonConstant;
import org.edu.common.system.base.controller.BaseController;
import org.edu.common.api.vo.Result;
import org.edu.common.system.query.QueryGenerator;
import org.edu.common.util.oConvertUtils;
import org.edu.modules.jxgz.api.CommonApi;
import org.edu.modules.jxgz.util.CommonAipUtils;
import org.edu.modules.jxrw.entity.VeJwJxrw;
import org.edu.modules.jxrw.service.IVeJwJxrwService;
import org.edu.modules.pyfa.api.IVeJwJxzyFeignClientService;
import org.edu.modules.pyfa.config.ConfigConstant;
import org.edu.modules.pyfa.entity.*;
import org.edu.modules.pyfa.service.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.edu.common.aspect.annotation.AutoLog;

/**
 * @Description: 培养方案
 * @Author: jeecg-boot
 * @Date: 2021-06-12
 * @Version: V1.0
 */
@Api(tags = "培养方案")
@RestController
@RequestMapping("/jxgz/veJwPyfa")
@Slf4j
public class VeJwPyfaController extends BaseController<VeJwPyfa, IVeJwPyfaService> {
    @Autowired
    private IVeJwPyfaService veJwPyfaService;
    @Autowired
    private IVeJwKcjhService veJwKcjhService;
    @Autowired
    private IVeJwXkjhService veJwXkjhService;
    @Autowired
    private IVeJwJxrwService veJwJxrwService;
    @Autowired
    private IVeJwJxbService veJwJxbService;
    @Autowired
    private IVeJwJxzyFeignClientService veJwJxzyFeignClientService;
    @Autowired
    private CommonApi commonApi;


    /**
     * 分页列表查询
     *
     * @param veJwPyfa
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "培养方案-分页列表查询")
    @ApiOperation(value = "培养方案-分页列表查询", notes = "培养方案-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(VeJwPyfa veJwPyfa,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<VeJwPyfa> queryWrapper = QueryGenerator.initQueryWrapper(veJwPyfa, req.getParameterMap());
        queryWrapper.eq("del_flag", 0);
        Page<VeJwPyfa> page = new Page<VeJwPyfa>(pageNo, pageSize);
        IPage<VeJwPyfa> pageList = veJwPyfaService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 回收站分页列表查询
     *
     * @param veJwPyfa
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "培养方案-回收站分页列表查询")
    @ApiOperation(value = "培养方案-回收站分页列表查询", notes = "培养方案-回收站分页列表查询")
    @GetMapping(value = "/rlist")
    public Result<?> queryPageRList(VeJwPyfa veJwPyfa,
                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                    HttpServletRequest req) {
        QueryWrapper<VeJwPyfa> queryWrapper = QueryGenerator.initQueryWrapper(veJwPyfa, req.getParameterMap());
        queryWrapper.eq("del_flag", 1);
        Page<VeJwPyfa> page = new Page<VeJwPyfa>(pageNo, pageSize);
        IPage<VeJwPyfa> pageList = veJwPyfaService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 回收站分页列表查询
     *
     * @param veJwJxb
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "培养方案-获取教学班")
    @ApiOperation(value = "培养方案-获取教学班", notes = "培养方案-获取教学班")
    @GetMapping(value = "/jxbList")
    public Result<?> jxbList(VeJwJxb veJwJxb,
                             @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                             @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                             HttpServletRequest req) {
        QueryWrapper<VeJwJxb> queryWrapper = QueryGenerator.initQueryWrapper(veJwJxb, req.getParameterMap());
        queryWrapper.eq("isdelete", 0);
        Page<VeJwJxb> page = new Page<VeJwJxb>(pageNo, pageSize);
        IPage<VeJwJxb> pageList = veJwJxbService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 查找是否有符合條件培养方案
     *
     * @param veJwKcjh
     * @return
     */
    @AutoLog(value = "培养方案-查找生成教学任务")
    @ApiOperation(value = "培养方案-查找生成教学任务", notes = "培养方案-查找生成教学任务")
    @PostMapping(value = "/checkPyfa")
    public Result<?> checkPyfa(@RequestBody VeJwKcjh veJwKcjh) {
        if (veJwKcjh.getGradeid() == null || veJwKcjh.getGradeid() == 0) {
            return Result.error("请选择学年！");
        }
        if (veJwKcjh.getSemid() == null || veJwKcjh.getSemid() == 0) {
            return Result.error("请选择学期！");
        }
        if (veJwKcjh.getFalid() == null || veJwKcjh.getFalid() == 0) {
            return Result.error("请选择院系！");
        }
        if (veJwKcjh.getSpecid() == null || veJwKcjh.getSpecid() == 0) {
            return Result.error("请选择专业！");
        }
        veJwKcjh.setIsAble(1);
        LambdaQueryWrapper<VeJwKcjh> queryWrapper = QueryGenerator.initQueryWrapper(veJwKcjh, new HashMap<>()).lambda();
        List<VeJwKcjh> jwKcjhList = veJwKcjhService.list(queryWrapper);
        if (jwKcjhList.size() == 0) {
            return Result.error("未找到相关课程计划，无法生成教学任务！");
        }

        Map<String, String> teacherNameMap = new HashMap<>();
        //生成教学任务操作
        Result<List<JSONObject>> banjiList = commonApi.queryBanJiListBySpecId(CommonApi.COMMON_TOKEN_KEY, veJwKcjh.getSpecid().toString());
        if (banjiList.isSuccess()) {
            if (banjiList.getResult() == null || banjiList.getResult().size() == 0) {
                return Result.error("对应专业下无班级，无法生成教学任务！");
            }
            int banjiNum = 0;
            //将对应班级信息生成教学班，对应循环生成教学任务，班级数*（课程+选课计划）
            for (JSONObject banji : banjiList.getResult()) {
//                VeJwJxb jwJxb = new VeJwJxb();
//                jwJxb.setFalid(veJwKcjh.getFalid());//院系
//                jwJxb.setSemid(veJwKcjh.getSemid());//学期
//                jwJxb.setSpecid(banji.getInteger("specId"));//专业
//                jwJxb.setGradeid(banji.getInteger("gradeId"));//年级
////                跳过不对应年级的班级
//                if(!jwJxb.getGradeid().equals(veJwKcjh.getGradeid())){
//                    continue;
//                }
//                jwJxb.setJzid(banji.getInteger("jzid"));
//                jwJxb.setJsid(banji.getInteger("jsid"));
//                jwJxb.setCampusid(banji.getInteger("campusId"));
//                jwJxb.setXzbids("," + banji.getInteger("id") + ",");
//
//                jwJxb.setIsmergedclass(0);
//                jwJxb.setIsdelete(0);
//                jwJxb.setType(0);
//                jwJxb.setTerminalid(1);
//
//                QueryWrapper<VeJwJxb> veJwJxbQueryWrapper = QueryGenerator.initQueryWrapper(jwJxb, new HashMap<>());
//                veJwJxbQueryWrapper.last(" limit 1 ");
//                VeJwJxb oldJxb = veJwJxbService.getOne(veJwJxbQueryWrapper);

//                if (oldJxb == null) {
//                    jwJxb.setXzbnames(banji.getString("xzbmc"));
//                    jwJxb.setBjmc(banji.getString("xzbmc"));
//                    jwJxb.setCreatetime(Integer.parseInt(System.currentTimeMillis() / 1000 + ""));
//                    veJwJxbService.save(jwJxb);
//                } else {
//                    jwJxb = oldJxb;
//                }
                Integer banjiId = banji.getInteger("id");
                String xzbmc = banji.getString("xzbmc");
                Map<Integer, String> xueqiMap = CommonAipUtils.getXueqiIdName();
                Map<Integer, String> xuezhiMap = CommonAipUtils.getXuezhiIdNameMap();
                banjiNum++;
//				生成课程计划的教学任务
                for (VeJwKcjh item : jwKcjhList) {
//                    String name = jwJxb.getBjmc()
                    String name = xzbmc
                            + "-"
                            + veJwJxzyFeignClientService.getKcNameById(item.getKcid())
                            + "-"
                            + (xueqiMap.containsKey(item.getSemid()) ? xueqiMap.get(item.getSemid()) : null)
                            + "-"
                            + (xuezhiMap.containsKey(item.getXzid()) ? xuezhiMap.get(item.getXzid()) : null)
                            + "-"
                            + "必修课教学任务";
                    VeJwJxrw jxrw = new VeJwJxrw();
                    jxrw.setName(name);
                    jxrw.setSemid(veJwKcjh.getSemid());
                    jxrw.setGradeid(veJwKcjh.getGradeid());
                    jxrw.setFalid(item.getFalid());
                    jxrw.setSpecid(item.getSpecid());
//                    jxrw.setXzbids(jwJxb.getXzbids());
                    jxrw.setXzbids("," + banjiId + ",");
                    jxrw.setXzid(item.getXzid());
                    jxrw.setKcid(item.getKcid());
                    jxrw.setKcxz(item.getKcxzm());
                    jxrw.setJslxid(item.getJslx());
                    jxrw.setWtime(item.getZks() == null ? 0 : item.getZks());

                    jxrw.setKcxf(item.getScore() == null ? new BigDecimal(0) : item.getScore());
                    jxrw.setZxs(item.getZxs());

                    String jhjs = item.getJhjs() != null ? item.getJhjs().split(",")[0] : null;
                    jxrw.setJsid(StringUtils.isNotEmpty(jhjs) ? Integer.valueOf(jhjs) : null);

                    String tearcher = item.getTeachers() != null ? item.getTeachers().split(",")[0] : null;
                    jxrw.setTeacheruserid(tearcher);
                    if (tearcher != null) {
                        if (!teacherNameMap.containsKey(tearcher)) {
                            teacherNameMap.put(tearcher, CommonAipUtils.getTeacherName(tearcher));
                        }
                        jxrw.setTeachername(teacherNameMap.get(tearcher));
                    }
                    //必修
                    jxrw.setType(ConfigConstant.TYPE_BIXIU);
                    jxrw.setCheckstatus(ConfigConstant.STATUS_AGREE);
                    jxrw.setIspublish(ConfigConstant.NO_PUSH);
//                    jxrw.setJxbid(jwJxb.getId());
                    jxrw.setJxbid(banjiId);
                    jxrw.setJxjhid(item.getId());
                    jxrw.setDelflag(CommonConstant.DEL_FLAG_0);
                    veJwJxrwService.save(jxrw);
                }
            }
            if (banjiNum == 0) {
                return Result.error("对应专业下无相关年级的班级，无法生成教学任务！");
            }
        } else {
            return Result.error("获取专业下班级失败！");
        }
        return Result.ok("生成教学任务成功！");
    }


    /**
     * 查找是否有符合條件培养方案
     *
     * @param veJwPyfa
     * @return
     */
    @AutoLog(value = "培养方案-查找生成教学任务")
    @ApiOperation(value = "培养方案-查找生成教学任务", notes = "培养方案-查找生成教学任务")
    @PostMapping(value = "/checkPyfaOld")
    public Result<?> checkPyfaOld(@RequestBody VeJwPyfa veJwPyfa) {
        if (veJwPyfa.getGradeid() == null || veJwPyfa.getGradeid() == 0) {
            return Result.error("请选择学年！");
        }
        if (veJwPyfa.getSemid() == null || veJwPyfa.getSemid() == 0) {
            return Result.error("请选择学期！");
        }
        if (veJwPyfa.getFalid() == null || veJwPyfa.getFalid() == 0) {
            return Result.error("请选择院系！");
        }
        if (veJwPyfa.getSpecid() == null || veJwPyfa.getSpecid() == 0) {
            return Result.error("请选择专业！");
        }
        veJwPyfa.setIsAble(1);
        veJwPyfa.setDelFlag(CommonConstant.DEL_FLAG_0);
        LambdaQueryWrapper<VeJwPyfa> queryWrapper = QueryGenerator.initQueryWrapper(veJwPyfa, new HashMap<>())
                .orderByDesc("id").last(" limit 1").lambda();

        VeJwPyfa target = veJwPyfaService.getOne(queryWrapper);
        if (target == null) {
            return Result.error("没有查找到符合的培养方案，无法生成教学任务！");
        }

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("pyfa_id", target.getId());

        List<VeJwKcjh> jwKcjhList = veJwKcjhService.list(wrapper);
        List<VeJwXkjh> jwXkjhList = veJwXkjhService.list(wrapper);
        if (jwKcjhList.size() == 0 && jwXkjhList.size() == 0) {
            return Result.error("对应培养方案下无课程计划，无法生成教学任务！");
        }

        //生成教学任务操作
        Result<List<JSONObject>> banjiList = commonApi.queryBanJiListBySpecId(CommonApi.COMMON_TOKEN_KEY, veJwPyfa.getSpecid().toString());
        if (banjiList.isSuccess()) {
            if (banjiList.getResult() == null || banjiList.getResult().size() == 0) {
                return Result.error("对应专业下无班级，无法生成教学任务！");
            }
            //将对应班级信息生成教学班，对应循环生成教学任务，班级数*（课程+选课计划）
            for (JSONObject banji : banjiList.getResult()) {
//                VeJwJxb jwJxb = new VeJwJxb();
//                jwJxb.setFalid(target.getFalid());//院系
//                jwJxb.setSemid(target.getSemid());//学期
//                jwJxb.setSpecid(banji.getInteger("specId"));//专业
//                jwJxb.setGradeid(target.getGradeid());//年级
//                jwJxb.setJzid(banji.getInteger("jzid"));
//                jwJxb.setJsid(banji.getInteger("jsid"));
//                jwJxb.setCampusid(banji.getInteger("campusId"));
//                jwJxb.setXzbids("," + banji.getInteger("id") + ",");
//
//                jwJxb.setIsmergedclass(0);
//                jwJxb.setIsdelete(0);
//                jwJxb.setType(0);
//                jwJxb.setTerminalid(1);
//
//                QueryWrapper<VeJwJxb> veJwJxbQueryWrapper = QueryGenerator.initQueryWrapper(jwJxb, new HashMap<>());
//                veJwJxbQueryWrapper.last(" limit 1 ");
//                VeJwJxb oldJxb = veJwJxbService.getOne(veJwJxbQueryWrapper);
//
//                if (oldJxb == null) {
//                    jwJxb.setXzbnames(banji.getString("xzbmc"));
//                    jwJxb.setBjmc(banji.getString("xzbmc"));
//                    jwJxb.setCreatetime(Integer.parseInt(System.currentTimeMillis() / 1000 + ""));
//                    veJwJxbService.save(jwJxb);
//                } else {
//                    jwJxb = oldJxb;
//                }

                Map<Integer, String> xueqiMap = CommonAipUtils.getXueqiIdName();
                Map<Integer, String> xuezhiMap = CommonAipUtils.getXuezhiIdNameMap();

                Integer banjiId = banji.getInteger("id");
                String xzbmc = banji.getString("xzbmc");
//				生成课程计划的教学任务
                for (VeJwKcjh item : jwKcjhList) {
//                    String name = jwJxb.getBjmc()
                    String name = xzbmc
                            + "-"
                            + veJwJxzyFeignClientService.getKcNameById(item.getKcid())
                            + "-"
                            + (xueqiMap.containsKey(target.getSemid()) ? xueqiMap.get(target.getSemid()) : null)
                            + "-"
                            + (xuezhiMap.containsKey(target.getXzid()) ? xueqiMap.get(target.getXzid()) : null)
                            + "-"
                            + "必修课教学任务";
                    VeJwJxrw jxrw = new VeJwJxrw();
                    jxrw.setName(name);
                    jxrw.setSemid(target.getSemid());
                    jxrw.setGradeid(target.getGradeid());
                    jxrw.setFalid(item.getFalid());
                    jxrw.setSpecid(item.getSpecid());
//                    jxrw.setXzbids(jwJxb.getXzbids());
                    jxrw.setXzbids("," + banjiId + ",");
                    jxrw.setXzid(target.getXzid());
                    jxrw.setKcid(item.getKcid());
                    jxrw.setKcxz(item.getKcxzm());
                    jxrw.setJslxid(item.getJslx());
                    jxrw.setWtime(item.getZks() == null ? 0 : item.getZks());

                    jxrw.setKcxf(item.getScore() == null ? new BigDecimal(0) : item.getScore());
                    jxrw.setZxs(item.getZxs());

                    String tearchId = item.getTeachers() != null ? item.getTeachers().split(",")[0] : null;
                    jxrw.setJsid(StringUtils.isNotEmpty(tearchId) ? Integer.valueOf(tearchId) : null);
                    //必修
                    jxrw.setType(ConfigConstant.TYPE_BIXIU);
                    jxrw.setCheckstatus(ConfigConstant.STATUS_AGREE);
                    jxrw.setIspublish(ConfigConstant.NO_PUSH);
//                    jxrw.setJxbid(jwJxb.getId());
                    jxrw.setJxbid(banjiId);
                    jxrw.setJxjhid(target.getId());
                    jxrw.setDelflag(CommonConstant.DEL_FLAG_0);
                    veJwJxrwService.save(jxrw);
                }

//				生成选课计划的教学任务
                for (VeJwXkjh item : jwXkjhList) {
//                    String name = jwJxb.getBjmc()
                    String name = xzbmc
                            + "-"
                            + veJwJxzyFeignClientService.getKcNameById(item.getKcid())
                            + "-"
                            + (xueqiMap.containsKey(target.getSemid()) ? xueqiMap.get(target.getSemid()) : null)
                            + "-"
                            + (xuezhiMap.containsKey(target.getXzid()) ? xueqiMap.get(target.getXzid()) : null)
                            + "-"
                            + "选修课教学任务";
                    VeJwJxrw jxrw = new VeJwJxrw();
                    jxrw.setSemid(target.getSemid());
                    jxrw.setGradeid(target.getGradeid());
                    jxrw.setFalid(item.getFalid());
                    jxrw.setSpecid(item.getSpecid());
//                    jxrw.setXzbids(jwJxb.getXzbids());
                    jxrw.setXzbids("," + banjiId + ",");
                    jxrw.setXzid(target.getXzid());
                    jxrw.setKcid(item.getKcid());
                    jxrw.setKcxz(item.getKcxzm());
                    jxrw.setJslxid(item.getJslx());
                    jxrw.setWtime(item.getZks() == null ? 0 : item.getZks());

                    jxrw.setKcxf(item.getScore() == null ? new BigDecimal(0) : item.getScore());
                    jxrw.setZxs(item.getZxs());

                    String tearchId = item.getTeachers() != null ? item.getTeachers().split(",")[0] : null;
                    jxrw.setJsid(StringUtils.isNotEmpty(tearchId) ? Integer.valueOf(tearchId) : null);
                    //选修
                    jxrw.setType(ConfigConstant.TYPE_XUANXIU);
                    jxrw.setCheckstatus(ConfigConstant.STATUS_AGREE);
                    jxrw.setIspublish(ConfigConstant.NO_PUSH);
//                    jxrw.setJxbid(jwJxb.getId());
                    jxrw.setJxbid(banjiId);
                    jxrw.setJxjhid(target.getId());
                    jxrw.setDelflag(CommonConstant.DEL_FLAG_0);
                    veJwJxrwService.save(jxrw);
                }
            }
        } else {
            return Result.error("获取专业下班级失败！");
        }
        return Result.ok("生成教学任务成功！");
    }


    /**
     * 分页列表查询有效信息
     *
     * @param veJwPyfa
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "培养方案-分页列表查询有效信息")
    @ApiOperation(value = "培养方案-分页列表查询有效信息", notes = "培养方案-分页列表查询有效信息")
    @GetMapping(value = "/tlist")
    public Result<?> queryPageTList(VeJwPyfa veJwPyfa,
                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                    HttpServletRequest req) {
        QueryWrapper<VeJwPyfa> queryWrapper = QueryGenerator.initQueryWrapper(veJwPyfa, req.getParameterMap());
        queryWrapper.eq("del_flag", 0).eq("is_able", 1);
        Page<VeJwPyfa> page = new Page<VeJwPyfa>(pageNo, pageSize);
        IPage<VeJwPyfa> pageList = veJwPyfaService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param veJwPyfa
     * @return
     */
    @AutoLog(value = "培养方案-添加")
    @ApiOperation(value = "培养方案-添加", notes = "培养方案-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody VeJwPyfa veJwPyfa) {
        veJwPyfaService.save(veJwPyfa);
        for (VeJwKcjh kcjh : veJwPyfa.getChlidKcjhList()) {
            kcjh.setPyfaId(veJwPyfa.getId());
            veJwKcjhService.saveOrUpdate(kcjh);
        }
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param veJwPyfa
     * @return
     */
    @AutoLog(value = "培养方案-编辑")
    @ApiOperation(value = "培养方案-编辑", notes = "培养方案-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody VeJwPyfa veJwPyfa) {
        veJwPyfaService.updateById(veJwPyfa);
        if (veJwPyfa.getId() != null && (veJwPyfa.getChlidKcjhList() == null || veJwPyfa.getChlidKcjhList().size() == 0)) {
            LambdaQueryWrapper<VeJwKcjh> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(VeJwKcjh::getPyfaId, veJwPyfa.getId());
            veJwKcjhService.remove(queryWrapper);
        } else if ((veJwPyfa.getChlidKcjhList() != null && veJwPyfa.getChlidKcjhList().size() > 0)) {
            List<Integer> ids = new ArrayList<>();
            for (VeJwKcjh kcjh : veJwPyfa.getChlidKcjhList()) {
                if (kcjh.getId() != null) {
                    veJwKcjhService.saveOrUpdate(kcjh);
                    ids.add(kcjh.getId());
                    veJwKcjhService.updateById(kcjh);
                }
            }
//			删除额外的数据
            LambdaQueryWrapper<VeJwKcjh> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(VeJwKcjh::getPyfaId, veJwPyfa.getId());
            if (ids.size() > 0) {
                queryWrapper.notIn(VeJwKcjh::getId, ids);
            }
            veJwKcjhService.remove(queryWrapper);
        }
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "培养方案-通过id删除")
    @ApiOperation(value = "培养方案-通过id删除", notes = "培养方案-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        if (veJwPyfaService.getById(id) != null) {
            veJwPyfaService.updateById(veJwPyfaService.getById(id).setDelFlag(1));
            return Result.ok("删除成功!");
        }
        return Result.error("参数错误！");
    }

    /**
     * 批量启用禁用
     *
     * @param jsonObject
     * @return
     */
    @AutoLog(value = "成绩类型-启用禁用")
    @ApiOperation(value = "成绩类型-启用禁用", notes = "成绩类型-启用禁用")
    @PutMapping(value = "/stateBatch")
    public Result<?> stateBatch(@RequestBody JSONObject jsonObject) {
        if (!jsonObject.containsKey("ids")) {
            return Result.error("参数错误!");
        }
        if (!jsonObject.containsKey("state")) {
            return Result.error("参数错误!");
        }
        String ids = jsonObject.getString("ids");
        Integer state = jsonObject.getInteger("state");

        VeJwPyfa data = new VeJwPyfa();
        data.setIsAble(state);

        LambdaQueryWrapper<VeJwPyfa> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(VeJwPyfa::getId, Arrays.asList(ids.split(",")));
        veJwPyfaService.update(data, queryWrapper);
        return Result.ok(state == 1 ? "启用成功!" : "禁用成功");
    }

    /**
     * 通过id销毁
     *
     * @param id
     * @return
     */
    @AutoLog(value = "培养方案-通过id销毁")
    @ApiOperation(value = "培养方案-通过id销毁", notes = "培养方案-通过id销毁")
    @DeleteMapping(value = "/destroy")
    public Result<?> destroy(@RequestParam(name = "id", required = true) String id) {
        veJwPyfaService.removeById(id);
        return Result.ok("销毁成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "培养方案-批量删除")
    @ApiOperation(value = "培养方案-批量删除", notes = "培养方案-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {

        List<String> list = Arrays.asList(ids.split(","));
        VeJwPyfa pyfa = null;
        for (String id : list) {
            pyfa = veJwPyfaService.getById(id);
            if (pyfa != null) {
                veJwPyfaService.updateById(pyfa.setDelFlag(1));
            }
        }
        return Result.ok("批量删除成功!");
    }


    /**
     * 批量销毁
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "培养方案-批量销毁")
    @ApiOperation(value = "培养方案-批量销毁", notes = "培养方案-批量销毁")
    @DeleteMapping(value = "/destroyBatch")
    public Result<?> destroyBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.veJwPyfaService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量销毁成功!");
    }


    /**
     * 通过id还原
     *
     * @param id
     * @return
     */
    @AutoLog(value = "培养方案-通过id还原")
    @ApiOperation(value = "培养方案-通过id还原", notes = "培养方案-通过id还原")
    @GetMapping(value = "/restore")
    public Result<?> restore(@RequestParam(name = "id", required = true) String id) {
        if (veJwPyfaService.getById(id) != null) {
            veJwPyfaService.updateById(veJwPyfaService.getById(id).setDelFlag(0));
            return Result.ok("还原成功!");
        }
        return Result.error("参数错误！");

    }

    /**
     * 批量还原
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "培养方案-批量还原")
    @ApiOperation(value = "培养方案-批量还原", notes = "培养方案-批量还原")
    @GetMapping(value = "/restoreBatch")
    public Result<?> restoreBatch(@RequestParam(name = "ids", required = true) String ids) {
        List<String> list = Arrays.asList(ids.split(","));
        VeJwPyfa pyfa = null;
        for (String id : list) {
            pyfa = veJwPyfaService.getById(id);
            if (pyfa != null) {
                veJwPyfaService.updateById(pyfa.setDelFlag(0));
            } else {
                return Result.error("参数错误！");
            }
        }
        return Result.ok("批量还原成功!");

    }


    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "培养方案-通过id查询")
    @ApiOperation(value = "培养方案-通过id查询", notes = "培养方案-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        VeJwPyfa veJwPyfa = veJwPyfaService.getById(id);
        if (veJwPyfa == null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(veJwPyfa);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param veJwPyfa
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, VeJwPyfa veJwPyfa) {
        return super.exportXls(request, veJwPyfa, VeJwPyfa.class, "培养方案");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, VeJwPyfa.class);
    }


    /**
     * 更新毕业状态
     *
     * @param veJwPyfa
     * @return
     */
    @AutoLog(value = "培养方案-更新毕业状态")
    @ApiOperation(value = "培养方案-更新毕业状态", notes = "培养方案-更新毕业状态")
    @PostMapping(value = "/updateIsGraduate")
    public Result<?> updateIsGraduate(@RequestBody VeJwPyfa veJwPyfa) {
        VeJwPyfa item = veJwPyfaService.getById(veJwPyfa.getId());
        if (item.getIsGraduate() == 0) {
            veJwPyfaService.updateById(item.setIsGraduate(1));
        } else {
            veJwPyfaService.updateById(item.setIsGraduate(0));
        }

        return Result.ok("更新毕业状态成功！");
    }


    /**
     * 更新有效状态
     *
     * @param veJwPyfa
     * @return
     */
    @AutoLog(value = "培养方案-更新有效状态")
    @ApiOperation(value = "培养方案-更新有效状态", notes = "培养方案-更新有效状态")
    @PostMapping(value = "/updateIsAble")
    public Result<?> updateIsAble(@RequestBody VeJwPyfa veJwPyfa) {
        VeJwPyfa item = veJwPyfaService.getById(veJwPyfa.getId());
        if (item.getIsAble() == 0) {
            veJwPyfaService.updateById(item.setIsAble(1));
        } else {
            veJwPyfaService.updateById(item.setIsAble(0));
        }

        return Result.ok("更新有效状态成功！");
    }


}
